/**
 * 
 */
package messaage;

import forum.Thread;

/**
 * @author Olga
 * 
 */
public class CreatedThreadMsg extends Message {

	protected String threadContent;
        private boolean friendOrNot;
        private Thread thread;
        private String forumID;
        private String date;

	/**
	 * 
	 */
	public CreatedThreadMsg(String username, String threadContent) {
		super.username = username;
		this.threadContent = threadContent;
	}
	
	public CreatedThreadMsg(String msg){
		parse(msg);
	}
	
	public CreatedThreadMsg(Thread t, String forumID, boolean isFriend){
		this.thread = t;
		this.friendOrNot = isFriend;
		this.username = t.getAuthor();
		this.forumID = forumID;
		this.date = t.getRoot().getDate();
		this.threadContent = t.getRoot().getContent();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see messaage.Message#toString()
	 */
	@Override
	public String toString() {
		//return "CreatedThread\n" + this.threadContent + "\n\0";
		String ans = "";
		ans += "CreatedThread\n";
		ans += this.friendOrNot + "\n";
		ans += this.forumID + "\n";
		ans += "ThreadNode\n";
		ans += "PostNode\n";
		ans += this.thread.getRoot().getID() + "\n";
		ans += this.thread.getRoot().getSubject() + "\n";
		ans += this.thread.getRoot().getAuthor() + "\n";
		ans += this.thread.getRoot().getDate() + "\n";
		ans += this.thread.getRoot().getContent() + "\n";
		return ans + "\0";
	}

	/**
	 * @return the threadContent
	 */
	public String getThreadContent() {
		return threadContent;
	}

        private void parse(String msg) {
            String [] info = msg.split("\n");
            if (info[1].equals("true"))
                this.friendOrNot = true;
            else
                this.friendOrNot = false;
            this.forumID = info[2];
            String rootPostID = info[5];
            String subject = info[6];
            String author = info[7];
            String dateString = info[8];
            String body = "";
            for (int i = 9; i < info.length-1; i++){
                body += info[i] + "\n";
            }
            this.thread = new Thread(Integer.parseInt(rootPostID), subject, body, author);
            this.thread.getRoot().setDate(dateString);
        }

        public boolean getIsFriend(){
            return this.friendOrNot;
        }

        public Thread getThread(){
            return this.thread;
        }

        public int getForumID(){
            return Integer.parseInt(forumID);
        }

        public String getDate(){
            return this.date;
        }



	
}
